using System;
using System.Collections.Generic;
using System.Text;
using IGlobalist.Cms.DataAccess;
using IGlobalist.Cms.Model;

namespace IGlobalist.Cms.Bll
{
    public class ContentNodeController :BaseController
    {
        private static IContentNote ConvertCmsObject2ContentNote(ICmsObject obj)
        {
            IContentNote contentNote = new ContentNote();
            contentNote.FromCmsObject(obj);
            contentNote.AuthorName = GetNameByAuthor(contentNote.Author);
            return contentNote;
        }


        private static IList<IContentNote> ConvertListObject2ListContentNotes(IList<ICmsObject> list)
        {
            IList<IContentNote> contentNotes = new List<IContentNote>();
            if (list != null)
            foreach (ICmsObject obj in list)
            {
                contentNotes.Add(ConvertCmsObject2ContentNote(obj));
            }

            return contentNotes;
        }


        private static ContentNoteList ConvertListObject2ContentNoteList(IList<ICmsObject> list)
        {
            ContentNoteList contentNotes = new ContentNoteList();
            if (list != null)
                foreach (ICmsObject contentNote in list)
                {
                    contentNote.Tags = GetNameByAuthor(contentNote.Author);
                    contentNotes.Add(ConvertCmsObject2ContentNote(contentNote));

                }
            return contentNotes;
        }

        public static IContentNote GetContentNote(long id)
        {
            return ConvertCmsObject2ContentNote(_Ds.GetObject(id));
        }


        public static IList<IContentNote> GetContentNoteList(long classID)
        {
            return ConvertListObject2ListContentNotes(_Ds.GetObjectListByClassID(classID));
        }




        public static ContentNoteList GetContentNoteListLelateToObjectID(long objectID)
        {
            return ConvertListObject2ContentNoteList(_Ds.GetObjectListRelateToObjectID(objectID, (short)CmsObjectType.ContentNote));
        }

        public static long InsertContentNote(IContentNote contentNote)
        {
            long id = _Ds.InsertObject((ICmsObject)contentNote);
            //if (contentNote.ParentID > 0)
            //{
            //    _Ds.InsertObjectRelation(contentNote.ParentID, id, (short)CmsObjectType.Content);
            //}
            return id;
        }

        public static void UpdateContentNote(IContentNote contentNote)
        {
            _Ds.UpdateObject((ICmsObject)contentNote);

           
            
        }

        public static void DeleteContentNote(long id)
        {
            _Ds.DeleteObject(id);

        }
        private static string GetNameByAuthor(long author)
        {
            IUser u = new User();
            u = UserController.GetUser(author);
            return u.Name;
        }
    }
}
